package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.myannotation.RequiredLogin;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.StrategyStatisVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategiesController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IRedisService redisService;

    @Autowired
    private StringRedisTemplate template;


    @GetMapping("/query")
    public Object query(StrategyQueryObject qo){
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes(){
        List<StrategyTheme> strategyThemes = strategyThemeService.selectAll();
        return JsonResult.success(strategyThemes);
    }

    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.selectOne(id);
        ////评论数加一
       strategyService.viewnum(strategy);
        return JsonResult.success(strategy);
    }

    //处理攻略评论的输入
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        //在保存评论的过程中实现回复数加一
        Strategy strategy = strategyService.selectOne(comment.getStrategyId());
        strategyService.replyNum(strategy);
        //塞数据之前需要对字段进行维护
        BeanUtils.copyProperties(userInfo1, comment);
        //还需对用户id进行维护
        comment.setUserId(userInfo1.getId());
        //还需设置评论的创建时间
        comment.setCreateTime(new Date());
        //再进行评论的保存
        strategyCommentService.insert(comment);
        return JsonResult.success();
    }

    //攻略评论内容的回显
    @GetMapping("/comments")
    public Object comments(StrategyCommentQueryObject qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    //处理点赞功能
    @PostMapping("/commentThumb")
    @RequiredLogin
    public Object commentThumb(String cid,HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        StrategyComment comment=null;
        //判断是否有登录用户，若有登录用户，则调用点赞功能业务
        if(userInfo1!=null){
            strategyCommentService.commentThumb(cid);
        }
        return JsonResult.success();
    }

    //统计阅读数
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        Strategy strategy = strategyService.selectOne(sid);
        //获取vo对象
        StrategyStatisVO strategyStatisVO = strategyService.getStrategyStatisVO(strategy);
        return JsonResult.success(strategyStatisVO);
    }

    //统计收藏数
    @PostMapping("/favor")
    @RequiredLogin
    public Object favor(String sid,HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        String userId = userInfo1.getId();
        boolean b=strategyService.favorNum(sid,userId);
        return JsonResult.success(b);
    }

    //统计点赞数
    @PostMapping("/strategyThumbup")
    @RequiredLogin
    public Object strategyThumbup(String sid,HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        String userId = userInfo1.getId();
        boolean b = strategyService.strategyThumbup(sid,userId);
        return JsonResult.success(b);
    }
}
